$(function(){

	// On Move To Action
	$("#act-move-to").click(function(e){
		// Display Directory Popup
		var messageIds = selectedMessagesId();
		$.ajax({
			type: 'POST',
			url: '?mod=messages&act=moveto&xvtrs=none',
			data : '&messageids='+messageIds,
			success: function(data) {
				$result = $('<div>'+data+'</div>');
				$('#site').append($result);
				initializeBehaviors($result);
			}
		});
	});
	// On Delete
	$("#act-delete").click(function(e){
		
		$('body').append('<div id="modal" class="ui-widget-overlay" style="height:100%;z-index: 1001;"></div>');
		$.ajax({
			type: 'POST',
			url: '?mod=messages&act=delete&xvtrs=none',
			data : '&messageids='+selectedMessagesId(),
			success: function(data) {
				try {
					data = JSON.parse(data);
					$("#modal").remove();
				} catch (e) {
					
				}
				$.fn.foxmessage(data);
				$('#contentCol').reload();
				$('#modal').remove();
			}
		});
	});
	// On Ban
	$("#act-banish").click(function(e){
		$('body').append('<div id="modal" class="ui-widget-overlay" style="height:100%;z-index: 1001;"></div>');
		$.ajax({
			type: 'POST',
			url: '?mod=messages&act=banish&sac=execute&xvtrs=none',
			data : '&messageids='+selectedMessagesId(),
			success: function(data) {
				try {
					data = JSON.parse(data);
					$("#modal").remove();
				} catch (e) {
					
				}
				$.fn.foxmessage(data);
				$('#contentCol').reload();
				$('#modal').remove();
			}
		});
	});
	// On Archive
	$("#act-archive").click(function(e){
		$.ajax({
			type: 'POST',
			url: '?mod=messages&act=archive&xvtrs=none',
			data : '&messageids='+selectedMessagesId(),
			success: function(data) {
				try {
					data = JSON.parse(data);
				} catch (e) {
					
				}
				$.fn.foxmessage(data);
				$("#contentCol tr.selected").remove();
			}
		});
	});
	// On Archive
	$("#act-parse-as-new,#act-parse-as-existing,#act-auto-parse").click(function(e){
		var messageids = selectedMessagesId();
		if(messageids == '') {
			return false;
		}
		$('body').append('<div id="modal" class="ui-widget-overlay" style="height:100%;z-index: 1001;"></div>');
		$.ajax({
			type: 'POST',
			url: '?mod=messages&act=contacts&sac=parse&xvtrs=none&mode='+$(this).attr('id'),
			data : '&messageids='+messageids,
			success: function(data) {
				try {
					data = JSON.parse(data);
					$.fn.foxmessage(data);
					$('#contentCol').reload();
					$('#modal').remove();
				} catch (e) {
					$result = $('<div>'+data+'</div>');
					$('#site').append($result);
					initializeBehaviors($result);
				}
			}
		});
	});
});

function selectedMessagesId() {
	// Find all selected messages
	var messageIds = '';
	$("#contentCol tr.selected").each(function(){
		messageIds += $(this).attr('id').replace('messages_','')+':';
	});
	return messageIds;
}